package com.example.javademo;

import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


public class MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_menu);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 方式一：使用 registerForContextMenu 注册上下文菜单（需配合 onCreateContextMenu 和 onContextItemSelected 使用）
        // registerForContextMenu(findViewById(R.id.default_activity_button));

        // 方式二：使用 ActionMode 启动上下文操作模式（代码方式）-- 实现后，长按，头部菜单栏显示菜单
        Button btn = findViewById(R.id.default_activity_button);
        btn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.d("tag", "onLongClick: 长按");
                startActionMode(actionModeCallback); // 手动启动 ActionMode
                return true; // 返回 true 表示事件已消费
            }
        });
//        弹出式菜单
        final Button popupButton = findViewById(R.id.popup_button);
        popupButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.d("tag", "onClick: 弹出式菜单");
                PopupMenu popupMenu = new PopupMenu(MenuActivity.this, v);
//                加载菜单资源
//                getMenuInflater() 获取菜单填充
//                inflate(R.menu.popup, popupMenu.getMenu()) 将 XML 中定义的菜单项加载到 PopupMenu 中
                popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
//                  设置监听器
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getItemId() == R.id.copy) {
                            Toast.makeText(MenuActivity.this, "点击了选项1", Toast.LENGTH_SHORT).show();
                            return true;
                        } else if (item.getItemId() == R.id.paste) {
                            Toast.makeText(MenuActivity.this, "点击了选项2", Toast.LENGTH_SHORT).show();
                            return true;
                        }
                        return false;
                    }
                });
//                popupMenu.inflate(R.menu.context);
                popupMenu.show();
            }
        });
    }

    /*
    *  收到创建optionMenu
    * */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.option, menu);
        /*
        * 参数：
        * menu：菜单对象
        * groupId：菜单项的组ID
        * itemId：菜单项的ID
        * order：菜单项的顺序
        * title：菜单项的标题
        *
        * */
        menu.add(0, 1, 0, "菜单项1");
         SubMenu subMenu = menu.addSubMenu(0, 2, 1, "菜单项2");
        subMenu.add(1, 3, 0, "菜单项3");


        return true;
    }

    // ActionMode 回调对象，用于上下文操作模式
    ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
        // 创建 ActionMode 时调用
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            Log.e("TAG", "onCreateActionMode: 创建");
            mode.getMenuInflater().inflate(R.menu.context, menu); // 加载上下文菜单资源
            return true;
        }

        // ActionMode 创建后调用（用于更新菜单）
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            Log.e("TAG", "onPrepareActionMode: 准备");
            return false; // 不更新菜单
        }

        // ActionMode 中菜单项被点击
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (item.getItemId() == R.id.rename) {
                Log.d("TAG", "onActionItemClicked: 重命名");
                Toast.makeText(MenuActivity.this, "点击了菜单项1（重命名）", Toast.LENGTH_SHORT).show();
                return true;
            }
            if (item.getItemId() == R.id.delete) {
                Toast.makeText(MenuActivity.this, "点击了菜单项2（删除）", Toast.LENGTH_SHORT).show();
                return true;
            }
            if (item.getItemId() == R.id.menu_item_3) {
                Toast.makeText(MenuActivity.this, "点击了菜单项3", Toast.LENGTH_SHORT).show();
                return true;
            }
            return true;
        }

        // ActionMode 销毁时调用
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            Log.d("tag", "onDestroyActionMode: 销毁");
        }
    };

    // ----------------------------
    // 选项菜单（OptionMenu）相关方法
    // ----------------------------

    // 创建选项菜单（ActionBar）
    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.option, menu);
        return true;
    }*/

    // 创建上下文菜单（传统方式，配合 registerForContextMenu 使用）
    /*@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.option, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("菜单");
        menu.setHeaderIcon(R.drawable.ic_launcher_background);
    }*/

    // 处理上下文菜单项点击事件（与 registerForContextMenu 配合使用）
    /*@Override
    public boolean onContextItemSelected(@NotNull MenuItem item) {
        if (item.getItemId() == R.id.menu_item_1) {
            Log.d("TAG", "onContextItemSelected: 菜单项1");
            Toast.makeText(this, "点击了菜单项1", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (item.getItemId() == R.id.menu_item_2) {
            Toast.makeText(this, "点击了菜单项2", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onContextItemSelected(item);
    }*/

    // 处理选项菜单项点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*if (item.getItemId() == R.id.menu_item_1) {
            Log.d("TAG", "onOptionsItemSelected: 保存");
            Toast.makeText(this, "点击了菜单项1", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (item.getItemId() == R.id.menu_item_2) {
            Toast.makeText(this, "点击了菜单项2", Toast.LENGTH_SHORT).show();
            return true;
        }*/
        switch (item.getItemId()){
            case 1:
                Log.d("TAG", "onOptionsItemSelected: 菜单项1");
                Toast.makeText(this, "点击了菜单项1", Toast.LENGTH_SHORT).show();
                return true;
            case 2:
                Toast.makeText(this, "点击了菜单项2", Toast.LENGTH_SHORT).show();
                return true;

            case 3:
                Toast.makeText(this, "点击了菜单项3", Toast.LENGTH_SHORT).show();
                return true;
        }
        return true;
    }
}